// pages/refundDetail/refundDetail.js
const app = getApp()
var timeChange = require('../../utils/timeChange.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info:'',
    wavaImage: '/images/orderDetails_wave.png',
    clockImage: '/images/order_clock.png',
    succeedImage: '/images/order_succeed.png',
    failImage: '/images/order_loseEfficacy.png',
    descT: '',
    // 倒计时
    timer: '',
    stateT: '',
    referImage: '',
    firstDashedImage: '/images/imaginaryBlueLine.png',
    waitImage: '',
    secondDashedImage: '/images/imaginaryBlueLine.png',
    refundImage: '',
    thirdDashedImage: '/images/imaginaryBlueLine.png',
    finishImage: '',
    waitColor: '#969696',
    waitT: '等待审核',
    refundColor: '#969696',
    refundT: '退款',
    finishColor: '#969696',
    cancelBtnHidden: true,
    refundTime: '',
    createTime: '',
    refundReasonT: '',
  },
  // 加载数据
  loadData(srId) {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: app.baseNet + 'refund/findRefundInfo?',
      data: {
        srId: srId,
        refundStatus: 1,
      },
      method: 'GET',
      header: {
        'Content-Type': 'application/json'
      },
      success(res) {
        console.log(res.data)
        wx.hideLoading()
        if (res.data.messcode == '001') {
          that.judgeState(res.data.refundInfoMap.refundState, res.data.refundInfoMap.refundTime)
          that.judgeRefundState(res.data.refundInfoMap.applyRefundInfo, res.data.refundInfoMap.otherRefund)
          var refundTime = timeChange.formatTimeTwo(res.data.refundInfoMap.refundTime, 'Y-M-D h:m:s')
          var createTime = timeChange.formatTimeTwo(res.data.refundInfoMap.createTime, 'Y-M-D h:m:s')
          that.setData({
            info: res.data.refundInfoMap,
            refundTime: refundTime,
            createTime: createTime,
          })
        } else if (res.data.messcode == '002') {
          // 没有数据
        } else {
          // 服务器异常
        }
      },
    })
  },
  // 取消退款按钮的点击事件
  cancelBtnClick: function() {
    var that = this
    wx.showModal({
      title: '',
      content: '确定取消退款申请吗?',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({
            title: '加载中...',
          })
          wx.request({
            url: app.baseNet + 'orderInfo/confirmOrder?',
            data: {
              srId: that.data.info.srId,
              stsate: 8,
              soiId: that.data.info.soiId,
            },
            method: 'GET',
            header: {
              'Content-Type': 'application/json'
            },
            success(res) {
              console.log(res.data)
              wx.hideLoading()
              if (res.data.messcode == '001') {
                wx.showToast({
                  title: '订单取消成功',
                  duration: 1000
                })
                setTimeout(function () {
                  wx.navigateBack({
                  })
                }, 1000)
              } else if (res.data.messcode == '002') {
                // 没有数据
              } else {
                // 服务器异常
              }
            },
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.loadData(options.srId)
  },
  // 判断申请理由
  judgeRefundState(applyRefundInfo, otherReason) {
    var that = this;
    if(applyRefundInfo == 0) {
      that.setData({
        refundReasonT: "申请理由: 定错了"
      })
    } else if (applyRefundInfo == 1) {
      that.setData({
        refundReasonT: "申请理由: 改订其他房源"
      })
    } else if (applyRefundInfo == 2) {
      that.setData({
        refundReasonT: "申请理由: 行程有变"
      })
    } else if (applyRefundInfo == 3) {
      that.setData({
        refundReasonT: "申请理由: 房屋与描述不符"
      })
    } else if (applyRefundInfo == 4) {
      that.setData({
        refundReasonT: "申请理由: 入住出现问题"
      })
    } else if (applyRefundInfo == 5) {
      that.setData({
        refundReasonT: "申请理由: " + otherReason
      })
    }
  },
  // 判断条件的显示
  judgeState(refundState, refundDate) {
    var that = this;
    if (refundState == 0) {
      that.countdown(refundDate, 60 * 60 * 24 * 3, " 0天0:0:0", 2)
      that.setData({
        stateT: "待退款",
        referImage: "/images/order_selectedTure.png",
        waitImage: "/images/order_selectedTure.png",
        refundImage: "/images/order_refundClock.png",
        finishImage: "/images/order_refundClock.png",
        thirdDashedImage: "/images/imaginaryGrayLine.png",
        waitColor: '#40d8d7',
        cancelBtnHidden: false,
      })
    } else if (refundState == 1) {
      that.setData({
        descT: '申请已通过, 等待退款中',
        stateT: "退款中",
        referImage: "/images/order_selectedTure.png",
        waitImage: "/images/order_selectedTure.png",
        refundImage: "/images/order_selectedClock.png",
        finishImage: "/images/order_refundClock.png",
        waitColor: '#40d8d7',
        refundColor: '#40d8d7',
        refundT: "退款中",
      })
    } else if (refundState == 2) {
      that.setData({
        descT: '退款已完成',
        stateT: "退款成功",
        referImage: "/images/order_selectedTure.png",
        waitImage: "/images/order_selectedTure.png",
        refundImage: "/images/order_selectedTure.png",
        finishImage: "/images/order_selectedTure.png",
        waitColor: '#40d8d7',
        refundColor: '#40d8d7',
        finishColor: '#40d8d7',
        refundT: "退款成功",
      })
    } else if (refundState == 3) {
      that.setData({
        descT: '理由与事实不符, 不能退款',
        stateT: "审核不通过",
        referImage: "/images/order_selectedTure.png",
        waitImage: "/images/order_selectedCancel.png",
        refundImage: "/images/order_cancel.png",
        finishImage: "/images/order_cancel.png",
        secondDashedImage: "/images/imaginaryGrayLine.png",
        thirdDashedImage: "/images/imaginaryGrayLine.png",
        waitColor: '#40d8d7',
        waitT: "审核不通过"
      })
    } else if (refundState == 4) {
      that.setData({
        descT: '用户取消退款申请, 退款单关闭',
        stateT: "已关闭",
        referImage: "/images/order_selectedTure.png",
        waitImage: "/images/order_selectedTure.png",
        refundImage: "/images/order_selectedTure.png",
        finishImage: "/images/order_selectedTure.png",
        waitColor: '#40d8d7',
        refundColor: '#40d8d7',
        finishColor: '#40d8d7',
        waitT: "用户取消退款",
      })
    }
  },
  // 倒计时
  countdown(endTime, totalTime, timeType, timeFlag) {
    var that = this;
    var EndTime = endTime || [];
    var NowTime = Date.parse(new Date()) / 1000;
    var total_micro_second = EndTime + totalTime - NowTime || [];
    console.log('剩余时间：' + total_micro_second);
    var cutdownT = that.dateformat(total_micro_second, timeFlag)
    console.log(cutdownT)
    // 渲染倒计时时钟
    that.setData({
      descT: cutdownT
    });
    if (total_micro_second <= 0) {
      that.setData({
        descT: timeType
      });
      return;
    }
    that.setData({
      timer: setTimeout(function () {
        total_micro_second -= 1;
        that.countdown(endTime, totalTime, timeType, timeFlag);
      }
        , 1000)
    })
  },

  // 时间格式化输出，如11:03 25:19 每1s都会调用一次
  dateformat(micro_second, timeFlag) {
    // 总秒数
    var second = Math.floor(micro_second);
    // 天数
    var day = Math.floor(second / 3600 / 24);
    // 小时
    var hr = Math.floor(second / 3600 % 24);
    // 分钟
    var min = Math.floor(second / 60 % 60);
    // 秒
    var sec = Math.floor(second % 60);
    if (timeFlag == 1) {
      return hr + ":" + min + ":" + sec;
    } else if (timeFlag == 2) {
      return day + "天" + hr + ":" + min + ":" + sec;
    } else {
      return min + ":" + sec;
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearTimeout(this.data.timer)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})